/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import net.cellobject.javacodeformatter.log.FormatterLog;
import net.cellobject.javacodeformatter.view.FormatterView;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Handler to open the "Favorites" view
 * 
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class OpenFormatterViewHandler extends AbstractHandler {
	/**
	 * Open the favorites view.
	 */
	public Object execute(final ExecutionEvent event) throws ExecutionException {
		// Get the active window
		final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		if (window == null) {
			return null;
		}
		// Get the active page
		final IWorkbenchPage page = window.getActivePage();
		if (page == null) {
			return null;
		}
		// Open and activate the Favorites view
		try {
			page.showView(FormatterView.ID);
		} catch (final PartInitException e) {
			FormatterLog.logError("Failed to open the Favorites view", e);
		}
		return null;
	}
}
